/*
package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

*/
/**
 * 根据用户名，修改用户的密码，昵称，年龄
 * 程序启动后，要求用户顺序输入用户名，密码，昵称，年龄
 * 然后完成需求
 */
/*

public class PWDDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入用户名");
        String username = scanner.nextLine();

        System.out.println("请输入新的密码");
        String password = scanner.nextLine();

        System.out.println("请输入新的昵称");
        String nickname = scanner.nextLine();

        System.out.println("请输入新的年龄");
        int age = Integer.parseInt(scanner.nextLine());

        try (Connection connection = DBUtil.getConnection()) {
            // 使用 PreparedStatement 构建 SQL 语句
            String sql = "UPDATE user SET password = ?, nickname = ?, age = ? WHERE name = ?";
            try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
                pstmt.setString(1, password);
                pstmt.setString(2, nickname);
                pstmt.setInt(3, age);
                pstmt.setString(4, username);

                int num = pstmt.executeUpdate();
                if (num > 0) {
                    System.out.println("用户信息更新成功");
                } else {
                    System.out.println("未找到该用户或更新失败");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}*/
package jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 根据用户名，修改用户的密码，昵称，年龄
 * 程序启动后，要求用户顺序输入用户名，密码，昵称，年龄
 * 然后完成需求
 */
public class PWDDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("修改信息");
        System.out.println("请输入用户名");
        String name = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = Integer.parseInt(scanner.nextLine());
        System.out.println(name + "," + password + "," + nickname + "," + age);

        try (Connection connection = DBUtil.getConnection();) {
            Statement statement = connection.createStatement();
            String sql = "UPDATE user " +
                    "SET password='" + password + "',nickname='" + nickname + "',age=" + age + " " +
                    "WHERE name='" + name + "'";
            int num = statement.executeUpdate(sql);
            System.out.println(num > 0 ? "修改成功" : "修改失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
